//
//  TopicSpecialTableView.m
//  ThePaperBase
//
//  Created by 程小刚 on 2019/5/6.
//  Copyright © 2019年 scar1900. All rights reserved.
//

#import "LBNestScrollTableView.h"


@implementation LBNestScrollTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollStop:) name:kScrollStopNotificationName object:nil];
        self.canScroll = YES;
        self.showsVerticalScrollIndicator = NO;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}


-(void)setType:(SpecialTableViewType)type{
    _type = type;
    self.canScroll = type==SpecialTableViewTypeSub?NO:YES;   // 子table默认不可滚动
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    if (self.type==SpecialTableViewTypeMain) {   // 主table类型的需要兼容手势
        return YES;
    }
    return NO;
}


- (void)setContentOffset:(CGPoint)contentOffset{
    CGFloat y = contentOffset.y;
    [super  setContentOffset:contentOffset];
    if(self.type == SpecialTableViewTypeMain){   // main类型
        CGFloat stayPosition = self.tableHeaderView.frame.size.height;  // 默认停留的位置
        if ([self.delegate_StayPosition respondsToSelector:@selector(nestTableViewHeightForStayPosition:)]) {
            stayPosition = [self.delegate_StayPosition nestTableViewHeightForStayPosition:self];  // 获取到停留的位置
        }
        if(self.canScroll == YES){
            if(y > stayPosition){
                contentOffset.y = stayPosition;
                [super setContentOffset:contentOffset];
                self.canScroll = NO;
                // 发送通知，主类不可滚动
                [[NSNotificationCenter defaultCenter] postNotificationName:kScrollStopNotificationName object:self userInfo:nil];
            }
        }else{ // main禁止滚动
            contentOffset.y = stayPosition;
            [super setContentOffset:contentOffset];
        }
    }else if(self.type == SpecialTableViewTypeSub){ // sub类型
        if (self.forceCanScroll) {
            if(y < 0){
                contentOffset.y = 0;
                [super setContentOffset:contentOffset];
                // 发送通知，子类不可滚动
                [[NSNotificationCenter defaultCenter] postNotificationName:kScrollStopNotificationName object:self userInfo:nil];
            }
        }else {
            if(self.canScroll == YES){
                if(y < 0){
                    contentOffset.y = 0;
                    [super setContentOffset:contentOffset];
                    self.canScroll = NO;
                    // 发送通知，子类不可滚动
                    [[NSNotificationCenter defaultCenter] postNotificationName:kScrollStopNotificationName object:self userInfo:nil];
                }
            }else{ // sub禁止滚动
                contentOffset.y = 0;
                [super setContentOffset:contentOffset];
            }
        }
    }
}


- (void)scrollStop:(NSNotification *)notification{
    LBNestScrollTableView *table = notification.object;
    if(self != table){  // 发送通知的table和当前self不是同一个时，则需要滚动
        self.canScroll = YES;
    }
    // 把其他所有的sub都移动到顶部,除去主的，其他table皆不能滚动
    if (table.type == SpecialTableViewTypeSub && self.type == SpecialTableViewTypeSub) {
        [self setContentOffset:CGPointZero];
        self.canScroll = NO;
        self.forceCanScroll = NO;
    }
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
